//Ford Fulkerson MaxFlow by DFS
//poj1273Drainage Ditches
#include <cstdio>
#include <cstring>
const int M = 256;
const int INF = 100000000;

struct NetworkFlow {
	int adj[M][M];
	int n, s, t;
	bool vis[M];
	NetworkFlow(int sz) {
		n = sz;
		memset(adj, 0, sizeof(adj));
	}
	int maxflow(int st, int ed) {
		s = st; t = ed;
		int ret = 0;
		while (true) {
			memset(vis, 0, sizeof(vis));
			int inc = dfs(s, INF);
			if (!inc) break;
			ret += inc;
		}
		return ret;
	}
	int dfs(int at, int by) {
		if (at == t) return by;
		if (vis[at]) return 0;
		vis[at] = true;
		for (int i = 0; i < n; ++i)
			if (adj[at][i] > 0) {
				int inc = dfs(i, by <? adj[at][i]);
				if (inc > 0) {
					adj[at][i] -= inc;
					adj[i][at] += inc;
					return inc;
				}
			}
		return 0;
	}
};

int main() {
	int n, m, a, b, w;
	while (scanf("%d%d", &m, &n) != EOF) {
		NetworkFlow nf(n);
		for (int i = 0; i < m; ++ i) {
			scanf("%d%d%d", &a, &b, &w);
			nf.adj[a-1][b-1] += w;
		}
		printf("%d\n", nf.maxflow(0, n-1));
	}
	return 0;
}
